package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.SuccessData;
import com.jzg.jcptadmin.viewinterface.OffonLineTaskInterface;

import java.util.Map;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2016/4/1.
 */
public class OffOnlineTaskPresenter extends BasePresenter<OffonLineTaskInterface> {
    private DataManager dataManager;
    private OffonLineTaskInterface mView;
    private Subscription mSubscription;

    public OffOnlineTaskPresenter(DataManager dataManager) {
        this.dataManager = dataManager;
    }

    @Override
    public void attachView(OffonLineTaskInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }
    public void upLoadOffOnLineTaskData() {
        if (!AppContext.isNetWork) {
            mView.dismissDialog();
            return;
        }
        Map<String,String> params = mView.getParams();
        if (!checkSubmitInfo(params)) {
            mView.dismissDialog();
            return;
        }
        mSubscription = dataManager.upLoadOffOnLineTaskData(params)
                .observeOn(AndroidSchedulers.mainThread()).
                subscribeOn(Schedulers.io()).subscribe(new Observer<SuccessData>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if (e != null&& mView!=null) {
                    mView.showError(e.getMessage());
                }
            }

            @Override
            public void onNext(SuccessData data) {
               mView.showResult(data);

            }
        });
    }


}
